<?php
session_start();
require_once("../connection.php");
$user = $_SESSION['user'];

$id = $_POST['id'];
$value = $_POST['value'];
$arr = explode("_",$id);

if($arr[0] == "gpsub")
{
	$gpid = $arr[1];
	$fbid = $user;	
	
	if($value == "1")
	{
		$temp = mysql_query("SELECT * FROM site_subscribe WHERE `gpid`='$gpid' AND `fbid`='$fbid'");
		if(mysql_numrows($temp)!=0)  // that means there is no record whatsoever
			$res = mysql_query("UPDATE site_subscribe SET `del`='0' WHERE `fbid`='$fbid' AND `gpid`='$gpid'");
		else // means there is a record, hence set the del parameter to 0 whatever its initial value be
			$res = mysql_query("INSERT INTO site_subscribe (`fbid`,`gpid`,`del`) VALUES('$fbid','$gpid','0')");
		
		if(!$res) echo  mysql_error();
	}
	else if($value == "0")
	{
		$temp = mysql_query("SELECT * FROM site_subscribe WHERE `gpid`='$gpid' AND `fbid`='$fbid'");
		if(mysql_numrows($temp)!=0) // if this values is 0, then there is no need to do any further action
			$res = mysql_query("UPDATE site_subscribe SET `del`='1' WHERE `fbid`='$fbid' AND `gpid`='$gpid'");
		else 
			$res = mysql_query("INSERT INTO site_subscribe (`fbid`,`gpid`,`del`) VALUES('$fbid','$gpid','1')"); 
		
		if(!$res) echo  mysql_error();
	}		
}
?>